package com.mdxx.xczd.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.google.zxing.activity.CaptureActivity;
import com.jakewharton.rxbinding.view.RxView;
import com.mdxx.xczd.R;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.app.ConstantsValue;
import com.mdxx.xczd.app.UrlValue;
import com.mdxx.xczd.bean.BarCodeBean;
import com.mdxx.xczd.ui.base.BaseActivity;
import com.mdxx.xczd.ui.contract.FirstContract;
import com.mdxx.xczd.ui.model.FirstModel;
import com.mdxx.xczd.ui.presenter.FirstPresenter;
import com.mdxx.xczd.utils.GsonUtil;
import com.mdxx.xczd.utils.Net;
import com.mdxx.xczd.utils.SPUtil;
import com.mdxx.xczd.utils.ToastUtil;
import com.mdxx.xczd.utils.VersionUpdateUtil;
import com.orhanobut.logger.Logger;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

public class FirstActivity extends BaseActivity<FirstPresenter, FirstModel> implements FirstContract.View {

    @BindView(R.id.btn_first_login)
    TextView mLogin;
    @BindView(R.id.btn_first_barbode)
    TextView mBarCode;
    @BindView(R.id.btn_first_settings)
    TextView mSettings;
    private final int SCANER_CODE = 3;
    private final int TYPE_INIT_REQUEST = 1;
    private final int TYPE_BARCODE_REQUEST = 2;

    private static SynthesizerPlayer synthesizerPlayer;
    private static final String APPID = "appid=519328ab";


    @Override
    protected int getLayout() {
        return R.layout.activity_first;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void init() {
        Logger.d("init");
        /*initAndUpdata();*/
        initBingView();
        initUrl();
        init1();
       /* while(true){
            if(Net.isNetWorkAvailable(FirstActivity.this)){
                initUrl();
                break;
            }
            Logger.d("isNetWorkAvailable");
            try{
                Thread.sleep(1000);
            }catch (Exception e){

            }
        }*/

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Logger.d("onNewIntent");
    }

    @Override
    public void showLoading(String title) {}

    @Override
    public void stopLoading() {}

    @Override
    public void showErrorTip(String msg) {}

    @Override
    public void returnBarCodeData(int type, int code) {
        if(TYPE_INIT_REQUEST == type ){
            initAndUpdata();
        }else if(TYPE_BARCODE_REQUEST == type){
            if(code == 0){
                ToastUtil.showShort("注册成功，请后台绑定");
            }else{
                ToastUtil.showShort("成功");
                startActivity(SelectUserActivity.class);
            }
        }
    }

    @Override
    public void returnBindState(int code) {
        if(code == 1){
            startActivity(SelectUserActivity.class);
        }else{
            ToastUtil.showLong("该机器还没有绑定，请后台绑定后再登陆");
        }
    }

    @OnClick({R.id.btn_first_barbode,R.id.btn_first_settings})
    void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_first_barbode:
                barcode();
                break;

            case R.id.btn_first_settings:
               /* startActivity("com.android.settings", "com.android.settings.Settings");*/
                play("wifi连接");
                break;
        }
    }

    private void initBingView(){
        RxView.clicks(mLogin)
                .throttleFirst(2 , TimeUnit.SECONDS )
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        login();
                    }
                }) ;
    }

    private void initAndUpdata(){
        if(SPUtil.getSharedIntData("first")  == 1){
            App.barCodeBean = GsonUtil.jsonToBean(SPUtil.getSharedStringData("barcode"), BarCodeBean.class);
            UrlValue.init(App.barCodeBean);
            ConstantsValue.autoSize = App.barCodeBean.getAutoAlarmTime()/1000;
            ConstantsValue.distance = App.barCodeBean.getAutoAlarmMetre();
            new VersionUpdateUtil(this).checkVersion(0);
        }
    }

    private void initUrl(){
        String first_url = SPUtil.getSharedStringData("first_url");
        if(!TextUtils.isEmpty(first_url)){
            App.firstUrl =  first_url;
            mPresenter.requestBarCode(TYPE_INIT_REQUEST);
        }
    }

    private void login(){
        if(SPUtil.getSharedIntData("first") == 1){
            mPresenter.getBindState();
        }else{
            ToastUtil.showLong("该机器还没有注册，请先扫码注册");
            barcode();
        }
    }

    private void barcode(){
        startActivityForResult(CaptureActivity.class, SCANER_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK ) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Logger.d("scanResult--"+scanResult);
            App.firstUrl = scanResult+"/RelicProtectionAdmin/";
            SPUtil.setSharedStringData("first_url", App.firstUrl);
            mPresenter.requestBarCode(TYPE_BARCODE_REQUEST);
        }
    }

    private void init1(){
        synthesizerPlayer = SynthesizerPlayer.createSynthesizerPlayer(this, APPID);
        synthesizerPlayer.setVoiceName("vivixiaoyan");//在此设置语音播报的人选
    }

    public void play(String content){
        Logger.d("content---------"+content);
        synthesizerPlayer.playText(content, "ent=vivi21,bft=5",null);
    }
}
